module planegame{

    export class StartMenu extends egret.DisplayObjectContainer {
        
        private textfield: egret.TextField;

        public constructor(stageW :number, stageH :number) {
            super();
            this.init(stageW, stageH);
            this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
        }

        private init(stageW :number, stageH :number) {

            let icon = GameUtils.instance().createBitmapByName("egret_icon_png");
            this.addChild(icon);
            icon.x = 26;
            icon.y = 33;

            let line = new egret.Shape();
            line.graphics.lineStyle(2, 0xffffff);
            line.graphics.moveTo(0, 0);
            line.graphics.lineTo(0, 117);
            line.graphics.endFill();
            line.x = 172;
            line.y = 61;
            this.addChild(line);


            let colorLabel = new egret.TextField();
            colorLabel.textColor = 0xffffff;
            colorLabel.width = stageW - 172;
            colorLabel.textAlign = "center";
            colorLabel.text = "Hello Egret";
            colorLabel.size = 24;
            colorLabel.x = 172;
            colorLabel.y = 80;
            this.addChild(colorLabel);

            let textfield = new egret.TextField();
            
            this.addChild(textfield);
            textfield.x = -1;
            textfield.y = 135;
            egret.Tween.get(textfield).to({x:175}, 1000);
            textfield.text = "点击开始游戏";
            textfield.alpha = 1;
            textfield.width = stageW - 172;
            textfield.textAlign = egret.HorizontalAlign.CENTER;
            textfield.size = 24;
            textfield.textColor = 0xffffff;
            
            
            this.textfield = textfield;

        //    var tw = egret.Tween.get( textfield );
        //    tw.to( {x:272, y:235}, 1000 );

            //根据name关键字，异步获取一个json配置文件，name属性请参考resources/resource.json配置文件的内容。
            // Get asynchronously a json configuration file according to name keyword. As for the property of name please refer to the configuration file of resources/resource.json.
         //   RES.getResAsync("description_json", this.startAnimation, this);
        }

        private onAddToStage(event: egret.Event) {
            this.removeEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
            this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.end, this);

            this.textfield.touchEnabled = true;
            this.textfield.addEventListener( egret.TouchEvent.TOUCH_TAP, this.onTouchMenu, this );
        }

        /**
         * 描述文件加载成功，开始播放动画
         * Description file loading is successful, start to play the animation
         */
        private startAnimation(result: string[]) {
            let parser = new egret.HtmlTextParser();

            let textflowArr = result.map(text => parser.parse(text));
            let textfield = this.textfield;
            let count = -1;
            let change = () => {
                count++;
                if (count >= textflowArr.length) {
                    count = 0;
                }
                let textFlow = textflowArr[count];

                // 切换描述内容
                // Switch to described content
                textfield.textFlow = textFlow;
                let tw = egret.Tween.get(textfield);
                tw.to({ x:272, y:235 }, 1000);
                tw.wait(2000);
                tw.to({ x:172, y:135 }, 1000);
                tw.call(change, this);
            };

            change();
        }


        // chumo
        private onTouchMenu( evt:egret.TouchEvent ) {
            egret.log("StartMenu onTouch");
            this.dispatchEventWith("changeStageGame");
            //egret.Tween.get( plane ).to( {x:x,y:y}, 300, egret.Ease.sineIn );
        }
        

        // recycle
        public end() {
            this.removeEventListener(egret.Event.REMOVED_FROM_STAGE, this.end, this);
            this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);

            this.textfield.touchEnabled = false;
            if(this.textfield.hasEventListener(egret.TouchEvent.TOUCH_TAP)) {
                this.textfield.removeEventListener( egret.TouchEvent.TOUCH_TAP, this.onTouchMenu, this );
            }
        }
    }

}